﻿using System;
using System.Collections.Generic;
using System.Reflection;
using Unity.VisualScripting;

namespace Script
{
    public class PoolManager : DoDoveFramework.Singleton<PoolManager>
    {
        private Dictionary<string, IPool> _pools = new Dictionary<string, IPool>();

        protected override void Initialize()
        {
        }

        public T GetPool<T>() where T : class, IPool, new()
        {
            string typeKey = typeof(T).FullName;

            IPool pool = null;
            if (_pools.TryGetValue(typeKey, out pool))
            {
                // 强制转换为目标类型并返回（确保类型安全）
                if (pool is T result)
                {
                    return result;
                }
            }


            throw new Exception($"Pool {typeof(T).FullName} does not exist and could not be auto-registered.");
        }

        public bool IsContainPool(string name)
        {
            return _pools.ContainsKey(name);
        }

        public void RegisterPool(string name, IPool pool, string poolName, string prefabName)
        {
            if (IsContainPool(name)) return;
            
            pool.InitPool(poolName, prefabName);
            _pools.Add(name, pool);
        }

        public void RemovePool(string name)
        {
            _pools.Remove(name);
        }
    }
}